<?
$smarty=new Smarty();
if(!ANTIHACK) die("ANTIHACK...");
$smarty->assign("nameact","Quản lý quảng cáo");
if($_GET["ajax"]=="showhome")
{
	$id=$_GET["id"];
	$value=$_GET["value"];
	$q=query("select * from quangcao where id=$id");
	$r=numrows($q);
	if($r<1)
	{
		echo 2;
		exit;
	}	
	if(update(array("is_active" => $value),"quangcao","id=$id"))
		echo "0";
	else
		echo "1";
	exit;
}
$q=query("select * from other where code='position-ads'");
$f=fetch($q);
$pos=json_decode($f["content1"]);
$sel=query("select * from quangcao order by position asc");
$i=0;
while($fsel=fetch($sel))
{
	$i++;
	$fsel['count']=$i;
	for($j=0; $j<count($pos); $j++)
		if($fsel['position']==$pos[$j][0])
		{
			$fsel['pn']=$pos[$j][1];
			break;
		}
	$quangcao[]=$fsel;
}
$smarty->assign("quangcao",$quangcao);

if($_POST['save'])
{
	$name=addslashes($_POST['name']);
	$height=$_POST['height'];
	$position=$_POST['position'];
	$sort=intval($_POST['sort']);
	if($name =="")
		$smarty->assign("msg","Tên không được để trống");
	else if($height=="")
		$smarty->assign("msg","Chưa đặt chiều cao");
	else 
	{
		if($sort<0)
			$sort=1;
		$data=array(
			"name"=>$name,
			"height"=> $height,
			"position" =>$position,
			"sort" =>$sort,
		);
		$ret=insert($data,"quangcao");
		if($ret)
			$smarty->assign("msg","Thêm nhóm quảng cáo thành công"."<script>setTimeout('location.href=\"".base_admin."?act=quang-cao\"',1000);</script>");
		else
			$smarty->assign("msg","Có lỗi xảy ra");
	}
}
$smarty->assign("pos",$pos);
$smarty->display("quang-cao.htm");
?>